﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Common;
using MobileSales.Interface;
using System.Windows.Forms;

namespace MobileSales.Presenter
{
    public class SettingsPresenter 
    {
        private ISettingsView _view;
        private Settings settings;

        public SettingsPresenter(ISettingsView view)
        {
            settings = Settings.GetInstance();
            _view = view;
            _view.DbPassword = string.IsNullOrEmpty(settings.DatabasePassword)?string.Empty: settings.DatabasePassword;
            _view.DbPath = string.IsNullOrEmpty(settings.DatabasePath) ? string.Empty : settings.DatabasePath;
            _view.ExportPath = string.IsNullOrEmpty(settings.ExportPath) ? string.Empty : settings.ExportPath;
            _view.ImportPath = string.IsNullOrEmpty(settings.ImportPath) ? string.Empty : settings.ImportPath;
            if (settings.IsSaveFiles)
            {
                _view.IsSaveFile = true;
                _view.ArcExportPath = string.IsNullOrEmpty(settings.ArcExportPath) ? string.Empty : settings.ArcExportPath;
                _view.ArcImportPath = string.IsNullOrEmpty(settings.ArcImportPath) ? string.Empty : settings.ArcImportPath;
            }
            _view.CheckRemainingGoods = settings.CheckRemainingGoods;
            _view.VisibleNullGoods = settings.VisibleNullGoods;
            _view.SaveChanges += SaveSettings;
            _view.CancelChanges += CancelChanges;
        }

        public void SaveSettings(object sender, EventArgs e)
        {
            try
            {
                settings.DatabasePath = _view.DbPath;
                settings.DatabasePassword = _view.DbPassword;
                settings.ImportPath = _view.ImportPath;
                settings.ExportPath = _view.ExportPath;
                if (_view.IsSaveFile)
                {
                    settings.IsSaveFiles = true;
                    settings.ArcExportPath = _view.ArcExportPath;
                    settings.ArcImportPath = _view.ArcImportPath;
                }
                settings.VisibleNullGoods = _view.VisibleNullGoods;
                settings.CheckRemainingGoods = _view.CheckRemainingGoods;
                settings.Save();
            }
            catch (Exception)
            {
                MessageBox.Show("Помилка при збереженні змін!", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                                MessageBoxDefaultButton.Button1);
            }
        }

        public void CancelChanges (object sender, EventArgs e)
        {
            settings = Settings.GetInstance();
        }
    }
}
